package com.flyingocean;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame {
	JPanel mainPanel = new JPanel();
	JLabel label = new JLabel("爬取前请先删除同级目录下的Result.txt文件");
	JButton btn = new JButton("开始爬取");
	Main(){
		mainPanel.add(label);
		mainPanel.add(btn);
		btn.addMouseListener(new clickedLister());
		add(mainPanel);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(500, 500, 400, 200);
		setVisible(true);
	}
	private class clickedLister extends MouseAdapter{
		public void mouseClicked(MouseEvent event) {
			pullFromWeb pull = new pullFromWebImpl();
			pushIntoFile push = new pushIntoFileImpl();

			int cnt = 1;
			int success_cnt = 0;
			do {
				System.out.println("正在拉取并读入第"+cnt+"个页面");
				//拉取并初筛
				PlayerInfo p = pull.pull("http://www.stat-nba.com/player/" + Integer.toString(cnt) + ".html");
				//将初筛信息
				push.push(p);
				//实例化一个初筛文件处理器
				fileProccessor fp = new fileProccessor("data.txt");
				if(fp.ifSuccess) {
					success_cnt++;
				}
			} while (++cnt<4600);
			label.setText("共拉取页面数:"+(cnt-1)+"  "+"成功写入页面数: "+success_cnt);
			btn.setText("已完成读取! 再次点击将会重复写入!");
			
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("正在加载图形界面...");
		Main m = new Main();
		System.out.println("加载成功！");
	}
}